package com.cy.chatTest.controller;


import com.cy.chatTest.bin.BaseEntity;
import com.cy.chatTest.common.CommonUtil;
import com.cy.chatTest.common.StringUtils;
import com.cy.chatTest.db.UserInfo;
import com.cy.chatTest.db.UserInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class User {

    @Autowired
    UserInfoRepository userInfoRepository;


    /*@PostMapping(value = "/setHeadImage", produces = "application/json; charset=utf-8" )
    @ResponseBody
    public BaseEntity setHeadImage(MultipartFile file, HttpServletRequest httpServletRequest){

        if (file.isEmpty()) {
            return new BaseEntity(false,"文件不能为空");
        }
        UserInfo userInfo = CommonUtil.getUserInfo(httpServletRequest,userInfoRepository);

        System.out.println("图片大小："+file.getSize());
        // 获取文件的后缀名
        String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        System.out.println("上传的后缀名为：" + suffixName);
        // 文件上传后的路径
        String filePath = "D:/chatTest/headimg/";
        String fileName = UUID.randomUUID() + suffixName;

        File dest = new File(filePath+fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            //把老图干掉，回收空间
            String oldPath = "D:/chatTest"+userInfo.getImgUrl();
            File oldFile = new File(oldPath);
            if (oldFile.exists()){
                oldFile.delete();
            }


            userInfo.setImgUrl("/headimg/"+fileName);
            userInfoRepository.save(userInfo);
            Map<String,String> map = new HashMap<>();
            map.put("imgUrl","/headimg/"+fileName);
            //通知这个用户的所有好友，他的头像变了
            SocketTest.upDateContact(userInfo);
            return new BaseEntity(true,map);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BaseEntity(false,"文件上传失败!");
    }*/







    @PostMapping( "/setHeadImage" )
    @ResponseBody
    public BaseEntity setHeadImage(String imageName, HttpServletRequest httpServletRequest){

        if (StringUtils.isEmpty(imageName)){
            return new BaseEntity(false,"七牛上的图片名不能为空");
        }
        UserInfo userInfo = CommonUtil.getUserInfo(httpServletRequest,userInfoRepository);
        userInfo.setImgUrl(imageName);
        userInfoRepository.save(userInfo);
        //通知这个用户的所有好友，他的头像变了
        SocketTest.upDateContact(userInfo);
        return new BaseEntity(true);

    }

    @PostMapping("/searchUser")
    public BaseEntity searchUser(String email){
        UserInfo userInfo = userInfoRepository.findByUsername(email);
        return new BaseEntity(true,userInfo);
    }

    @PostMapping("/addFriend")
    @ResponseBody
    public BaseEntity addFriend(String email, HttpServletRequest httpServletRequest){
        UserInfo friend = userInfoRepository.findByUsername(email);
        UserInfo userInfo = CommonUtil.getUserInfo(httpServletRequest,userInfoRepository);
        if (friend==null || userInfo==null){
            return new BaseEntity(false,"参数错误");
        }else if (friend.getUsername().equals(userInfo.getUsername())){
            return new BaseEntity(false,"不能加自己为好友");
        }else {
            ArrayList<String> emails = userInfo.getContactsList();
            if (!emails.contains(friend.getUsername())){
                emails.add(friend.getUsername());
                userInfo.setContactsList(emails);
                userInfoRepository.save(userInfo);
                SocketTest.addContact(userInfo,friend);
            }else {
                return new BaseEntity(false,"对方已经是你的好友");
            }
            emails = friend.getContactsList();
            if (!emails.contains(userInfo.getUsername())){
                emails.add(userInfo.getUsername());
                friend.setContactsList(emails);
                userInfoRepository.save(friend);
                SocketTest.addContact(friend,userInfo);
            }
        }
        return new BaseEntity(true,userInfo);
    }

}
